package com.lzw.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzw.bean.*;
import com.lzw.bean.Menu;
import com.lzw.common.bean.Result;
import com.lzw.common.controller.BaseController;
import com.lzw.common.service.impl.CommonService;
import com.lzw.param.role.Page;
import com.lzw.service.MenuService;
import com.lzw.service.RoleMenuService;
import com.lzw.service.RoleService;
import com.lzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.awt.*;

/**
 * Created by 林志伟 on 2017/5/11.
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;




    @GetMapping("/index")
    public String  index(Model model, Page page){
        PageInfo pageInfo= roleService.page(page);
        logger.info("用户列表："+ JSON.toJSONString(pageInfo));
        model.addAttribute("pageInfo",pageInfo);
        return this.getTemplateName();
    }

    @GetMapping("/add")
    public String add(){
        return this.getTemplateName();
    }

    @PostMapping("/add")
    @ResponseBody
    public Result addPost(@Valid Role role, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
//            logger.info("验证错误："+bindingResult.getFieldError().getDefaultMessage());
            return CommonService.error(bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println("传入role:"+role.toString());
        Result result=roleService.insert(role);
        return result;
    }

    @GetMapping("/update")
    public String update(@RequestParam int id, Model model){
        Role role =roleService.find(id);
        model.addAttribute("role",role);
        return this.getTemplateName();
    }

    @ResponseBody
    @PostMapping("/update")
    public Result updatePost(Role role, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return CommonService.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return roleService.update(role);
    }

    @PostMapping("/delete")
    @ResponseBody
    public  Result delete(@RequestParam("id[]") int[] ids){
        for (int i=0;i<ids.length;i++){
            logger.info("删除ids:"+ids[i]);
        }
        return  roleService.deleteAll(ids);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/open")
    @ResponseBody
    public Result open(@RequestParam("id") int id){

        return  roleService.open(id);
    }


    /**
     * 禁用
     * @param id
     * @return
     */
    @PostMapping("/close")
    @ResponseBody
    public Result close(@RequestParam("id") int id){

        return  roleService.close(id);
    }

    @Autowired
    private MenuService menuService;

    @GetMapping("setMenu")
    public String setMenu(@RequestParam("role_id") int role_id, Model model){

        model.addAttribute("role_id",role_id);
        return  this.getTemplateName();
    }

    @PostMapping("getMenuByRole")
    @ResponseBody
    public Result getMenuByRole(@RequestParam("role_id") int role_id){
        return CommonService.success("查询成功",menuService.selectByRole(role_id));
    }

    private  @Autowired
    RoleMenuService roleMenuService;

    @PostMapping("setMenu")
    @ResponseBody
    public Result setMenuPost(@RequestParam("role_id") int role_id,@RequestParam("menu_ids[]") int[] menu_ids){
        return  roleMenuService.setMenu(role_id,menu_ids);
    }


}
